
package models;

import java.util.*;
import javax.persistence.*;
import play.data.format.*;
import play.data.validation.*;
import play.db.jpa.*;

@Entity
public class Premios {

    @Id
    @Constraints.Required
    public Integer numPremio;
    
    @Constraints.Required
    public String descPremio;
    
    @Constraints.Required
    public Integer codSorteo;
    
    @Constraints.Required
    public Integer cantidadPremio;
    
    @Constraints.Required
    public Integer garantiaPremio;
    
    @Constraints.Required
    public Float valorPremio;

    public Integer numParticipante;
    
    public static Premios findById(Integer numPremio) {
        return JPA.em().find(Premios.class, numPremio);
    }
    
    public void update(Integer numPremio) {
        this.numPremio = numPremio;
        JPA.em().merge(this);
    }
    
    public void save() {
        System.out.println("Grabando nuevo premio...");        
        JPA.em().persist(this);
    }
    public void delete() {
        JPA.em().remove(this);
    }
    
    public static Page page(int page, int pageSize, String sortBy, String order, int codSorteo) {
    

        if(sortBy==null) sortBy="descPremio";
        if(order==null) order="asc";
        
        if(page < 1) page = 1;
        Long total=0L;
       String query="from Premios p where lower(p.codSorteo) like ? order by p." + sortBy + " " + order;
       System.out.println(query);
            @SuppressWarnings("unchecked")
            List<Premios> data = JPA.em()
            .createQuery(query)
            .setParameter(1, codSorteo)
            .setFirstResult((page - 1) * pageSize)
            .setMaxResults(pageSize)
            .getResultList();
            total=Long.parseLong(data.size()+"");
        
        return new Page(data, total, page, pageSize);
    }
    
     public static class Page {
        
        private final int pageSize;
        private final long totalRowCount;
        private final int pageIndex;
        private final List<Premios> list;
        
        public Page(List<Premios> data, long total, int page, int pageSize) {
            this.list = data;
            this.totalRowCount = total;
            this.pageIndex = page;
            this.pageSize = pageSize;
        }
        
        public long getTotalRowCount() {
            return totalRowCount;
        }
        
        public int getPageIndex() {
            return pageIndex;
        }
        
        public List<Premios> getList() {
            return list;
        }
        
        public boolean hasPrev() {
            return pageIndex > 1;
        }
        
        public boolean hasNext() {
            return (totalRowCount/pageSize) >= pageIndex;
        }
        
        public String getDisplayXtoYofZ() {
            int start = ((pageIndex - 1) * pageSize + 1);
            int end = start + Math.min(pageSize, list.size()) - 1;
            return start + " al " + end + " de " + totalRowCount;
        }
        
    }
}


